﻿using System;


namespace VectorMath
{
    public class Pnt2D
    {
        public double x;
        public double y;

        public double SquaredLength { get { return Math.Pow(x, 2) + Math.Pow(y, 2); } }

        public double Length { get { return Math.Sqrt(SquaredLength); } }

        public Pnt2D()
            : this(0, 0)
        {

        }

        public Pnt2D(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public void Add(Pnt2D p)
        {
            x += p.x;
            y += p.y;
        }

        public void Sub(Pnt2D p)
        {
            x -= p.x;
            y -= p.y;
        }


        public void Times(double d)
        {
            x *= d;
            y *= d;
        }

        /// <summary>
        /// this = this + (p * d) .
        /// </summary>
        public void ScaleAdd(Pnt2D p, double d)
        {
            x += p.x * d;
            y += p.y * d;
        }

        /// <summary>
        /// Set this point's value.
        /// </summary>
        public void Set(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        /// <summary>
        /// Normalize this vector.
        /// </summary>
        public void Normalize()
        {
            double length = Length;
            x /= length;
            y /= length;
        }

        /// <summary>
        /// Returns this dot p.
        /// </summary>
        public double Dot(Pnt2D p)
        {
            return x * p.x + y * p.y;
        }

        /// <summary>
        /// Returns (this.x*p.y - this.y*p.x) which is in the Z direction.
        /// </summary>
        public double Cross(Pnt2D p)
        {
            return (x * p.y - y * p.x);
        }

        /// <summary>
        /// Returns a new point equal to a - b.
        /// </summary>
        public static Pnt2D Difference(Pnt2D a, Pnt2D b)
        {
            return new Pnt2D(a.x - b.x, a.y - b.y);
        }
    }
}
